var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tel: '',
    yzm: '',
    mfocus: '',
    yfocus: '',
    fsyzm: '',
    username: '',
    avatar: '',
    type:0,
    shopid:0,
    checked: false,
    xylist: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.type);
    console.log(options.shopid)
    if(options.type){
        this.setData({
          type:options.type,
          shopid:options.shopid
        })
    }
    this.getinfo(4);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  telInput: function (e) {
    this.setData({
      tel: e.detail.value
    })
  },
  yzmInput: function (e) {
    this.setData({
      yzm: e.detail.value
    })
  },
  mfocus: function () {
    this.setData({
      mfocus: 1
    })
  },
  mblur: function () {
    this.setData({
      mfocus: 0
    })
  },
  yfocus: function () {
    this.setData({
      yfocus: 1
    })
  },
  yblur: function () {
    this.setData({
      yfocus: 0
    })
  },

  formSubmit: function (e) {
    // 如果登录
    var that = this;
    var xx = wx.getStorageSync('userId');
    var checked = this.data.checked;
    if (that.data.tel == '' || that.data.yzm == '') {
      wx.showToast({
        title: '补全信息',
        icon: 'none',
        duration: 2000,
      })
      this.setData({
        fsyzm:0
      })
      return false;
    } else if (checked == false) {
      wx.showToast({
        title: '同意用户协议才能使用哦',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    var form = {
      url: 'api/user/bindMobile',
      data: {
        user_id:xx,
        mobile: that.data.tel,
        mobile_code: that.data.yzm,
        nickname: that.data.username,
        head: that.data.avatar,
      },
      success: function (e) {
        wx.showToast({
          title: e.data.msg,
          icon: 'none',
          duration: 3000
        })
        if (e.data.code == 1) {
          wx.setStorageSync('status', 1);
          if (that.data.type == 10) {
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/station/stationpay/stationpay?id=' + that.data.shopid,
              })
            }, 1000);
          }else if(that.data.type == 50){
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/shangjia/shangjia/shangjia',
              })
            }, 1000);
          } else if (that.data.type == 20) {
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/wash/washpay/washpay?id=' + that.data.shopid,
              })
            }, 1000);
          } else if(that.data.type == 30){
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/shangjia/shangjia-xx/shangjia-xx',
              })
            }, 1000);
          } else if (that.data.type == 40) {
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/shangjia/shangjia-xx2/shangjia-xx2',
              })
            }, 1000);
          } else{
            setTimeout(function () {
              // wx.switchTab({
              //   url: '/pages/mine/user/user',
              // })
              wx.redirectTo({
                url: '/pages/mine/user-xx/user-xx',
              })
            }, 1000);
          }
          
        }
      },
      fail:function(res){
        wx.showToast({
          title: '绑定失败',
          icon: 'none',
          duration: 2000
        }); 
      }
    };
    app.post(form);

  },

  // 获取用户信息
  onGotUserInfo: function () {
    if(this.data.tel == ''){
        wx.showToast({
          title: '你还没输入手机号呢',
          icon:'none',
          duration:3000
        })
        return false;
    }
    var that = this;
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              var username = res.userInfo.nickName;
              var avatar = res.userInfo.avatarUrl;
              that.setData({
                username: username,
                avatar: avatar
              });
              // 发送验证码
              var xx = wx.getStorageSync('userId');
              var yzm = {
                url: 'api/user/sendSms',
                data: {
                  mobile: that.data.tel,
                  user_id:xx
                },
                success: function (e) {
                  if (e.data.code == 1) {
                    wx.showToast({
                      title: '验证码已发送',
                      icon: 'none',
                      duration: 2000
                    })
                    that.setData({
                      fsyzm: 1
                    })
                  } else {
                    wx.showToast({
                      title: e.data.msg,
                      icon: 'none',
                      duration: 2000
                    })
                  }
                },
                fail: function (res) {
                  wx.showToast({
                    title: '绑定失败',
                    icon: 'none',
                    duration: 2000
                  });
                }
              };
              app.post(yzm);

            }
          });
        } else {
          wx.switchTab({
            url: '/pages/index/index',
          })
        }
      },
      fail: function () {
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    })
  },
  // 获取服务协议
  getinfo: function (inx) {
    var that = this;
    var param = {
      url: "api/shop/getArticles",
      success: function (e) {
        var data = e.data.data;
        console.log(data)
        that.setData({
          xylist: e.data.data[inx]
        })
      }
    };
    app.post(param);
  },
  xuzhi: function () {
    this.setData({
      checked: !this.data.checked
    })
  }

})